package pCloud;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.sun.xacml.Obligation;
import com.sun.xacml.attr.AttributeValue;
import com.sun.xacml.attr.StringAttribute;
import com.sun.xacml.ctx.Attribute;

public class Utilities {
	/**
	 * Parsing the resource and extract the path to the database
	 * @param resourceId: it has the form :..:..:/table
	 * @return
	 */
	public static String getDatabaseId(String resourceId){
		String[] s = resourceId.split("/");
		return s[0];
	}
	
	public static String getTableId(String resourceId){
		String[] s = resourceId.split("/");
		return s[1];
	}
	
	/**
	 * Get a list of attribute values for the specific attribute ID
	 * @return the value of the specified attribute, from the given set of attributes
	 * @throws URISyntaxException 
	 */
	public static ArrayList<AttributeValue> getAttributeValue(String attId, Set atts) throws URISyntaxException{
		Iterator it = atts.iterator();
		Attribute att;
		ArrayList<AttributeValue> values = new ArrayList<AttributeValue>(); 
		
		while (it.hasNext()){
			att = (Attribute)it.next();
			if (att.getId().equals(new URI(attId))){
				values.add(att.getValue());
			}
		}
		return values;
	}
	
	
	/**
	 * @param obligations
	 * @return the aggregation function, null if doesn't exist. We assume there's only 1 aggregation function
	 * in the obligations
	 * @throws Exception 
	 */
	public static String getAggregationFunction(Obligation obligation) throws Exception{
		if (obligation==null)
			return null;
		
		if (!obligation.getId().equals(new URI(PCloudAttributeAndType.OBLIGATION_COLUMN_AGG_ID)))
			return null;
		
		List assignments;
		Attribute att;
		assignments = obligation.getAssignments();			
		if (assignments.size()==0)
			return null;
		else
		{
			//go through all the assignment attribute and apply ones with the ID ...-aggregation-id
			for (int i=0; i<assignments.size(); i++){
				att = (Attribute)assignments.get(i);
				if (att.getId().equals(new URI(PCloudAttributeAndType.OBLIGATION_ATT_AGG))){
					return ((StringAttribute)att.getValue()).getValue();						
				}					
			}
		}
				
		return null;
	}
	
	
	public static String getDatabaseId(List<AttributeValue> values) throws Exception{
		if (values.size()==0) 
			throw new Exception("no datbase value");
		Iterator it = values.iterator();
		return ((StringAttribute)it.next()).getValue();
	}
	
	public static String getTableId(List<AttributeValue> values) throws Exception{
		if (values.size()==0) 
			throw new Exception("no table value");
		Iterator it = values.iterator();
		return ((StringAttribute)it.next()).getValue();
	}
	
	/**
	 * Return output of the form yyyy-MM-dd hh:mm:ss
	 */
	public static String parseCalendarOutput(Calendar cal){
		int month = cal.get(Calendar.MONTH)+1;
		return cal.get(Calendar.YEAR) + "-"+month+"-"+cal.get(Calendar.DAY_OF_MONTH)
		+ " "+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE)+":"+cal.get(Calendar.SECOND);
	}
}
